package app.itgungnir.compose.wan.business.main

import android.content.res.Configuration
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.layout.padding
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.material.icons.outlined.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import app.itgungnir.compose.wan.Routes
import app.itgungnir.compose.wan.business.main.agenda.AgendaScreen
import app.itgungnir.compose.wan.business.main.home.HomeScreen
import app.itgungnir.compose.wan.business.main.mine.MineScreen
import app.itgungnir.compose.wan.business.main.square.SquareScreen
import app.itgungnir.compose.wan.theme.WanComposePreviewTheme
import app.itgungnir.compose.wan.widgets.WanBadge
import com.google.accompanist.insets.navigationBarsPadding

@Composable
fun MainScreen(navController: NavHostController = rememberNavController(), activity: AppCompatActivity) {
    val viewModel = viewModel<MainViewModel>()
    val innerNavController: NavHostController = rememberNavController()
    val unreadMsgCount by viewModel.unreadMsgCountState.collectAsState()
    Scaffold(
        bottomBar = {
            val navBackStackEntry by innerNavController.currentBackStackEntryAsState()
            val currentDestination = navBackStackEntry?.destination
            BottomNavigation(
                modifier = Modifier.navigationBarsPadding(),
                backgroundColor = MaterialTheme.colors.background,
                elevation = 15.dp
            ) {
                MainPages.values().forEach { screen ->
                    val selected = currentDestination?.route == screen.route
                    val navIcon = when (selected) {
                        true -> screen.selectedIcon
                        else -> screen.normalIcon
                    }
                    BottomNavigationItem(
                        selected = selected,
                        onClick = {
                            if (selected) {
                                return@BottomNavigationItem
                            }
                            innerNavController.navigate(screen.route) {
                                popUpTo(innerNavController.graph.findStartDestination().id) { saveState = true }
                                restoreState = true
                            }
                        },
                        icon = {
                            if (screen == MainPages.Mine && unreadMsgCount > 0) {
                                WanBadge(
                                    badgeContent = { Text(text = "$unreadMsgCount") },
                                    hideBadgeWhenBadgeContentIsNull = true
                                ) {
                                    Icon(imageVector = navIcon, contentDescription = "")
                                }
                            } else {
                                Icon(imageVector = navIcon, contentDescription = "")
                            }
                        },
                        label = { Text(text = screen.title, style = MaterialTheme.typography.caption) },
                        selectedContentColor = MaterialTheme.colors.primary,
                        unselectedContentColor = MaterialTheme.colors.onSurface
                    )
                }
            }
        }
    ) { paddingValues ->
        NavHost(
            modifier = Modifier
                .padding(paddingValues)
                .navigationBarsPadding(bottom = false),
            navController = innerNavController,
            startDestination = Routes.MainRoutes.HOME
        ) {
            composable(route = Routes.MainRoutes.HOME) { HomeScreen(navController = navController) }
            composable(route = Routes.MainRoutes.SQUARE) { SquareScreen(navController = navController) }
            composable(route = Routes.MainRoutes.AGENDA) {
                AgendaScreen(navController = navController, activity = activity)
            }
            composable(route = Routes.MainRoutes.MINE) {
                MineScreen(navController = navController, parentViewModel = viewModel)
            }
        }
    }
}

private enum class MainPages(
    val route: String,
    val title: String,
    val normalIcon: ImageVector,
    val selectedIcon: ImageVector
) {
    Home(
        route = Routes.MainRoutes.HOME,
        title = "首页",
        normalIcon = Icons.Outlined.Home,
        selectedIcon = Icons.Filled.Home
    ),
    Square(
        route = Routes.MainRoutes.SQUARE,
        title = "广场",
        normalIcon = Icons.Outlined.Group,
        selectedIcon = Icons.Filled.Group
    ),
    Agenda(
        route = Routes.MainRoutes.AGENDA,
        title = "待办",
        normalIcon = Icons.Outlined.Bookmarks,
        selectedIcon = Icons.Filled.Bookmarks
    ),
    Mine(
        route = Routes.MainRoutes.MINE,
        title = "我的",
        normalIcon = Icons.Outlined.Person,
        selectedIcon = Icons.Filled.Person
    )
}

@Preview(name = "Light Mode")
@Preview(name = "Dark Mode", uiMode = Configuration.UI_MODE_NIGHT_YES, showBackground = true)
@Composable
private fun MainPreview() {
    WanComposePreviewTheme {
        MainScreen(activity = AppCompatActivity())
    }
}
